/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;


/**
 * Defines the current state of the GUI. Defines the elements to be loaded and removed from the window.
 */
public class GuiState {
	
	
	/**
	 * Callback function called when state is started.
	 */
	private ICallback start;
	
	
	/**
	 * Callback function called when state ends.
	 */
	private ICallback end;
	
	
	/**
	 * Current state.
	 */
	private State state;
	
	
	/**
	 * Enumerator of all possible GUI states.
	 */
	public enum State { 
		CONNECTION_SCREEN,
		WAITING_FOR_CONNECTION,
		CONNECTION_FAILURE,
		GAME_SETUP_SCREEN,
		CLEAN_INGAME_SCREEN,
		BANK,
		PALACE_OF_THE_CALIPH,
		TOWER_ELEMENT_MARKET,
		HOUSE_OF_SPIES,
		CONSTRUCTION_AUTHORITY,
		CHEATS,
		
		//For testing purposes
		TEST_MAP,
		TEST_TOWER_ELEMENTS
	};
	
	
	/**
	 * Constructor: Loads the corresponding start and end functions.
	 */
	public GuiState(State state) {
		this.state = state;
		
		
		
		if(state == State.CONNECTION_SCREEN) {
			start = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().show(State.CONNECTION_SCREEN);
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().hide();
				}
			};
			
			
			
		} else if(state == State.WAITING_FOR_CONNECTION) {
			start = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().show(State.WAITING_FOR_CONNECTION);
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().hide();
				}
			};
			
			
			
		} else if(state == State.CONNECTION_FAILURE) {
			start = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().show(State.CONNECTION_FAILURE);
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getConnectionScreen().hide();
				}
			};
			
			
			
		} else if(state == State.GAME_SETUP_SCREEN) {
			start = new ICallback() {
				public void run() {
					GUI.getGameSetupScreen().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getGameSetupScreen().hide();
				}
			};
			
			
		} else if(state == State.CLEAN_INGAME_SCREEN) {
			start = new ICallback() {
				public void run() {
					GUI.loadInGameScreen();
					GUI.getPanel().show();
					GUI.getPanel().startGame(); // Very important that this is last.
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getPanel().hide();
				}
			};
			
			
		} else if(state == State.BANK) {
			start = new ICallback() {
				public void run() {
					GUI.getBank().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getBank().hide();
				}
			};
			
		
		} else if(state == State.PALACE_OF_THE_CALIPH) {
			start = new ICallback() {
				public void run() {
					GUI.getPalaceOfTheCaliph().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getPalaceOfTheCaliph().hide();
				}
			};
			
			
		} else if(state == State.TOWER_ELEMENT_MARKET) {
			start = new ICallback() {
				public void run() {
					GUI.getTowerElementMarket().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getTowerElementMarket().hide();
				}
			};
			
			
		} else if(state == State.HOUSE_OF_SPIES) {
			start = new ICallback() {
				public void run() {
					GUI.getHouseOfSpies().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getHouseOfSpies().hide();
				}
			};
			
		
		} else if(state == State.CONSTRUCTION_AUTHORITY) {
			start = new ICallback() {
				public void run() {
					GUI.getConstructionAuthority().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getConstructionAuthority().hide();
				}
			};
			
			
		} else if(state == State.CHEATS) {
			start = new ICallback() {
				public void run() {
					GUI.getCheatScreen().show();
				}
			};
			end = new ICallback() {
				public void run() {
					GUI.getCheatScreen().hide();
				}
			};
			
			
		} else if(state == State.TEST_MAP) {
			start = new ICallback() {
				public void run() {
					Scenery.show();
				}
			};
			end = new ICallback() {
				public void run() {
				}
			};
			
			
			
		} else if(state == State.TEST_TOWER_ELEMENTS) {
			start = new ICallback() {
				public void run() {
					GUI.getNode().attachChild(new GuiWindow(GUI.Offset.WINDOW, 10, 10, 1004, 748, 50));
					float tempScale = 4.0f;
					int[] showElements = {0, 4, 8, 12, 16,   20, 24, 28, 32, 36,   40, 45, 50, 55, 60,   65, 69, 73, 77, 81};
					for(int i = 0; i < 20; i++) {
						TowerElement element = new TowerElement(showElements[i]);
						GuiTowerElement guiElement = new GuiTowerElement(element);
						GUI.getNode().attachChild(guiElement.get2D(GUI.Offset.WINDOW, 110 + (int)(50 * tempScale) * (i % 5), 700 - (int)(40 * tempScale) * (i / 5), 52, tempScale));
					}
				}
			};
			end = new ICallback() {
				public void run() {
				}
			};
			
			
			
		} else {
			System.err.println("ERROR: Non-implemented state.");
		}
		
	}
	
	
	/**
	 * Executes the start function of the current state.
	 */
	public void start() {
		start.run();
	}
	
	
	/**
	 * Executes the end function of the current state.
	 */
	public void end() {
		end.run();
	}


	/**
	 * Get state enumerator of this state.
	 */
	public State getState() {
		return state;
	}
	
	
	
}
